import 'package:path/path.dart' as path;
import 'package:xvideos/config/address.dart' as address;
import 'package:xvideos/hj_model/acg/comic_chapters_model.dart';
import 'package:xvideos/hj_model/video_model.dart';

//子集详情
class MediaContent {
  String? cover;
  int? countBrowse;
  int? countCollect;
  int? countComment;
  int? countDisLike;
  int? countLike;
  int? countPurchases;
  String? createdAt;
  int? episodeNumber;
  int? height;
  String? id;
  int? listenPermission; //  0:会员 1:金币购买 2:免费
  String? md5;
  String? mediaId;
  int? mediaSize;
  VidStatus? mediaStatus;
  String? name;
  int? playTime;
  int? price;
  int? ratio;
  String? text;
  String? updateTime;
  List<String>? urlSet;
  String? videoUrl;
  String? audioUrl;
  int? weight;
  bool? hasBuy;

  MediaContent({
    this.audioUrl,
    this.countBrowse,
    this.countCollect,
    this.countComment,
    this.countDisLike,
    this.countLike,
    this.countPurchases,
    this.createdAt,
    this.episodeNumber,
    this.height,
    this.id,
    this.listenPermission,
    this.md5,
    this.mediaId,
    this.mediaSize,
    this.mediaStatus,
    this.name,
    this.playTime,
    this.price,
    this.ratio,
    this.text,
    this.updateTime,
    this.urlSet,
    this.videoUrl,
    this.weight,
    this.hasBuy,
    this.cover,
  });

  MediaContent.fromJson(dynamic json) {
    countBrowse = json['countBrowse'];
    countCollect = json['countCollect'];
    countComment = json['countComment'];
    countDisLike = json['countDisLike'];
    countLike = json['countLike'];
    countPurchases = json['countPurchases'];
    createdAt = json['createdAt'];
    episodeNumber = json['episodeNumber'];
    height = json['height'];
    id = json['id'];
    listenPermission = json['listenPermission'];
    md5 = json['md5'];
    mediaId = json['mediaId'];
    mediaSize = json['mediaSize'];
    mediaStatus = json['mediaStatus'] != null ? VidStatus.fromMap(json['mediaStatus']) : null;
    name = json['name'];
    playTime = json['playTime'];
    price = json['price'];
    ratio = json['ratio'];
    text = json['text'];
    updateTime = json['updateTime'];
    urlSet = json['urlSet'] != null ? json['urlSet'].cast<String>() : [];
    videoUrl = json['videoUrl'];
    audioUrl = json['audioUrl'];
    weight = json['weight'];
    hasBuy = json['hasBuy'];
    cover = json['cover'];
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['countBrowse'] = countBrowse;
    map['countCollect'] = countCollect;
    map['countComment'] = countComment;
    map['countDisLike'] = countDisLike;
    map['countLike'] = countLike;
    map['countPurchases'] = countPurchases;
    map['createdAt'] = createdAt;
    map['episodeNumber'] = episodeNumber;
    map['height'] = height;
    map['id'] = id;
    map['listenPermission'] = listenPermission;
    map['md5'] = md5;
    map['mediaId'] = mediaId;
    map['mediaSize'] = mediaSize;
    if (mediaStatus != null) {
      map['mediaStatus'] = mediaStatus?.toJson();
    }
    map['name'] = name;
    map['playTime'] = playTime;
    map['price'] = price;
    map['ratio'] = ratio;
    map['text'] = text;
    map['updateTime'] = updateTime;
    map['urlSet'] = urlSet;
    map['videoUrl'] = videoUrl;
    map['audioUrl'] = audioUrl;
    map['weight'] = weight;
    map['hasBuy'] = hasBuy;
    map['cover'] = cover;

    return map;
  }

  ComicChapterInfo toComicChapterInfo() {
    ComicChapterInfo info = ComicChapterInfo();
    info.episodeNumber = episodeNumber;
    info.hasBuy = hasBuy;
    info.id = id;
    info.listenPermission = listenPermission;
    info.mediaId = mediaId;
    info.name = name;
    info.price = price;
    info.createdAt = createdAt;
    info.cover = cover;
    return info;
  }

  String get getRealAudioUrl {
    if (audioUrl == null || audioUrl!.isEmpty) {
      return "";
    }
    if (!audioUrl!.startsWith("http") && !audioUrl!.startsWith("https")) {
      return path.join(address.Address.audioCdnAddress ?? '', audioUrl);
      //return path.join(address.Address.cdnAddress ?? '', audioUrl);
    }

    return audioUrl!;
  }

  String get getRealVideoUrl {
    return "${address.Address.baseApiPath}/vid/h5/m3u8/$videoUrl?token=${address.Address.token}&cdn=${address.Address.cdnAddress}";
  }

  bool isCoinVideo() {
    if ((listenPermission ?? 0) == 0) {
      return true;
    }
    return false;
  }
}

//漫画播放记录历史
class ComicRecordDB {
  String? id; //子集id
  String? mId; //媒体id
  int? indexRecord; //观看的图片index
  int? allSet; //图片总数

  ComicRecordDB({
    this.id,
    this.mId,
    this.indexRecord,
    this.allSet,
  });

  ComicRecordDB.fromJson(dynamic json) {
    id = json['id'];
    mId = json['mId'];
    indexRecord = json['indexRecord'];
    allSet = json['allSet'];
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['mId'] = mId;
    map['indexRecord'] = indexRecord;
    map['allSet'] = allSet;
    return map;
  }
}
